package com.ptswitch.material.model.contract;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.validation.constraints.Size;

import org.apache.tapestry5.beaneditor.DataType;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hxzon.project.BaseEntity;
import org.hxzon.tapestry5.util.TapestryEx;
import org.hxzon.util.BigDecimalUtil;

import com.ptswitch.material.model.common.Uzer;

@Entity
public class ContractInvoice extends BaseEntity {
    public static final int Askfor_Invoice = 1;
    public static final int Reply_Invoice = 2;

    private static final long serialVersionUID = -7485172456471455798L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq_gen_continvo")
    @javax.persistence.SequenceGenerator(name = "id_seq_gen_continvo", sequenceName = "seq_id_continvo", allocationSize = 20)
    private long id;

    @ManyToOne
    @JoinColumn(name = "contractCode", referencedColumnName = "code", insertable = false, updatable = false)
    private Contract contract;

    @ColumnDefault(StringDefaultValue)
    @Size(max = DefineSize32)
    private String contractCode;

    @OneToOne(fetch = FetchType.EAGER)
    @NotFound(action = NotFoundAction.IGNORE)
    private ContractInvoice asker;

    @DataType(TapestryEx.DataType_currency)
    private BigDecimal payment = BigDecimalUtil.Int0;

    @ManyToOne(fetch = FetchType.EAGER)
    private Uzer sender;

    @ManyToOne(fetch = FetchType.EAGER)
    private Uzer reciever;

    @ColumnDefault(StringDefaultValue)
    @Size(max = DefineSize2048)
    private String remark;

    private boolean hadInvoice;

    private int invoiceType;

    // set and get

    public Contract getContract() {
        return contract;
    }

    public void setContract(Contract contract) {
        this.contract = contract;
        this.contractCode = contract.getCode();
    }

    public String getContractCode() {
        return contractCode;
    }

    public void setContractCode(String contractCode) {
        this.contractCode = contractCode;
    }

    public ContractInvoice getAsker() {
        return asker;
    }

    public void setAsker(ContractInvoice asker) {
        this.asker = asker;
    }

    public BigDecimal getPayment() {
        return payment;
    }

    public void setPayment(BigDecimal payment) {
        this.payment = payment;
    }

    public Uzer getSender() {
        return sender;
    }

    public void setSender(Uzer sender) {
        this.sender = sender;
    }

    public Uzer getReciever() {
        return reciever;
    }

    public void setReciever(Uzer reciever) {
        this.reciever = reciever;
    }

    public boolean isHadInvoice() {
        return hadInvoice;
    }

    public void setHadInvoice(boolean hadInvoice) {
        this.hadInvoice = hadInvoice;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public int getInvoiceType() {
        return invoiceType;
    }

    public void setInvoiceType(int invoiceType) {
        this.invoiceType = invoiceType;
    }

    //----transient for form-----------------
    private transient BigDecimal paymentCurrent;

    public BigDecimal getPaymentCurrent() {
        return paymentCurrent;
    }

    public void setPaymentCurrent(BigDecimal paymentCurrent) {
        this.paymentCurrent = paymentCurrent;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}
